const Message = require('../message')

/**
 * 数据返回控制器
 */
class ResponseController {

    setRoomId(roomId) {
        this._roomId = roomId;
        return this
    }

    setPeerId(peerId) {
        this._peerId = peerId;
        return this;
    }

    setClient(client) {
        this._client = client;
        return this
    }

    authorization() {
        this.send(Message.msgType.authorization, {})
    }

    /**
     * join 返回服务器支持的 媒体信息
     * @param rtpCapabilities
     */
    join({rtpCapabilities}) {
        this.send(Message.msgType.join, {
            rtpCapabilities
        })
    }

    /**
     * 创建 transport， 将连接信息返回给客户端， 客户端创建 sdp 时候使用
     * @param id
     * @param iceParameters 包含 ice
     * @param iceCandidates
     * @param dtlsParameters
     * @param sctpParameters
     * @param isConsumer
     */
    createTransport({id, iceParameters, iceCandidates, dtlsParameters, sctpParameters, isConsumer}) {
        this.send(Message.msgType.createTransport, {
            id,
            iceParameters,
            iceCandidates,
            dtlsParameters,
            sctpParameters,
            isConsumer
        })
    }

    /**
     * 通知客户端， 有新的生存者近来，客户端可以根据 信息，是否接入数据
     * @param id
     * @param producerId
     * @param kind
     * @param transportId
     * @param peerId
     * @param rtpParameters
     * @param type
     * @param appData
     * @param pause  是否状态是停止状态，只建立通道
     */
    newConsumer({id, producerId, kind, transportId, peerId, rtpParameters, type, appData, pause}, client) {
        client.send(Message.encoder(Message.msgType.newConsumer, {
            id, producerId, kind, transportId, peerId, rtpParameters, type, appData, pause
        }))
    }

    /**
     * 连接webrtc, 底层是 webrtc 数据连接
     * @param isConsumer 生产 或者 消费
     * @param transportId
     */
    connectWebRtcTransport({isConsumer, transportId}) {
        this.send(Message.msgType.connectWebRtcTransport, {
            isConsumer,
            transportId
        })
    }

    /**
     * 创建成功消费者， 返回给客户端
     * @param producerId
     * @param kind
     */
    producer({producerId, kind}) {
        this.send(Message.msgType.producer, {
            producerId,
            kind
        })
    }

    /**
     * 当消费者停止时候，告诉客户端，数据停止
     * @param producerId
     * @param consumerId
     * @param peerId
     * @param client
     */
    pauseConsumer({producerId, consumerId, peerId}, client) {
        client.send(Message.encoder(Message.msgType.pauseConsumer, {
            producerId,
            consumerId,
            peerId
        }))
    }

    /**
     * 当消费者恢复数据时候，告诉客户端，开始传送
     * @param producerId
     * @param consumerId
     * @param peerId
     * @param client
     */
    resumeConsumer({producerId, consumerId, peerId}, client) {
        client.send(Message.encoder(Message.msgType.resumeConsumer, {
            producerId,
            consumerId,
            peerId
        }))
    }

    /**
     * 获取房间所有用户
     * @param data
     */
    memberList(data) {
        this.send(Message.msgType.memberList, data)
    }

    notifiedMessage(client, message) {
        client.send(Message.encoder(Message.msgType.notified, {
            message: message
        }))
    }


    memberReceiveMessage(client, message) {
        client.send(Message.encoder(Message.msgType.receiveMessage, {
            message: message
        }))
    }

    error(error) {
        this.send(Message.msgType.error, {error: error})
    }

    pong(){
        this.send(Message.msgType.pong, {})
    }

    send(type, data) {
        this._client.send(Message.encoder(type, {
            ...data,
            peerId: this._peerId,
            roomId: this._roomId
        }))
    }
}

module.exports = {ResponseController}
